package com.gitee.huxing.course.netty.gateway.initializer;

import com.gitee.huxing.course.netty.gateway.adapter.GatewayChannelHandlerAdapter;
import com.gitee.huxing.course.netty.gateway.router.NettyRouter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.netty.gateway.initializer
 * @ClassName: GateWayChannelInitializer
 * @Author: huxing
 * @DateTime: 2021-08-21 下午3:02
 */
@Slf4j
public class GateWayChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 服务器编码格式
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
        pipeline.addLast(new GatewayChannelHandlerAdapter());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("这里打印异常记录信息:::" + "handlerRemoved");
        super.handlerRemoved(ctx);
    }
}
